package com.junzichangsheng.fileDownload;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class FileDownload {
    private String DEST_PATH;
    private String DEST_NAME_PREFIX;
    private String DEST_NAME_SUFFIX;

    public FileDownload(String FOLDER_NAME, String FILE_NAME_PREFIX, String FILE_NAME_SUFFIX) {
        //this.DEST_PATH = System.getProperty("user.home") + "\\Pictures\\漫画\\" + FOLDER_NAME + "\\";
        this.DEST_PATH = "G:\\乱七八糟\\_秘密\\漫画\\性福里\\" + FOLDER_NAME + "\\";
        this.DEST_NAME_PREFIX = FILE_NAME_PREFIX;
        this.DEST_NAME_SUFFIX = FILE_NAME_SUFFIX;
    }

    public void download() throws IOException {
        //去除重复数据
        List<String> srcPaths = getSrcPathsByFile();//根据图片地址
        //List<String> srcPaths = getSrcPathsByPath();//根据图片地址前缀遍历生成
        //List<String> srcPaths = getSrcPathsByXFL();//根据漫画主页生成-幸福里
        if(!srcPaths.isEmpty()){
            //获取序号最大长度
            int length = String.valueOf(srcPaths.size()).length();
            //遍历文件路径信息
            for(int i = 1;i <= srcPaths.size();i++){
                String zeroPrefix = String.format("%0" + length + "d",i);
                String srcPath = srcPaths.get(i - 1);
                InputStream inputStream = new URL(srcPath).openStream();
                byte[] bytes = IOUtils.toByteArray(inputStream);
                File file = new File(DEST_PATH + DEST_NAME_PREFIX + zeroPrefix + DEST_NAME_SUFFIX);
                FileUtils.writeByteArrayToFile(file,bytes);
            }
        }
    }

    private List<String> getSrcPathsByFile() throws IOException {
        List<String> srcPaths = new ArrayList<>();
        InputStream inputStream = ClassLoader.getSystemResourceAsStream("fileDownload/srcPaths.txt");
        if(inputStream != null){
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            String srcPath;
            while ((srcPath = reader.readLine()) != null) {
                srcPaths.add(srcPath);
            }
        }
        /*
        int max = 30;
        for(int i = 0;i<=max;i++){
            String t_num = String.format("%0" + 3 + "d",(i+204));
            String num = String.format("%0" + 2 + "d",i);
            //String srcPath = "https://m.acgnfl.com/24/06/content_25/504252/16ba380be2296be943e4e97c08b095b7.webp";
            //String srcPath = "https://m.acgnfl.com/24/06/content_25/504252/1.webp";
            //String srcPath = "https://m.acgnfl.com/24/06/content_25/504252/99.webp";
            String srcPath = "http://img.177picyy.com/uploads/2019/11a/a001-100.jpg" + num + ".jpg";
            srcPaths.add(srcPath);
        }
        */
        return deduplication(srcPaths);
    }

    private List<String> getSrcPathsByPath() throws IOException {
        List<String> srcPaths = new ArrayList<>();
        int startNum = 0;
        int endNum = 28;
        for(int i = startNum;i <= endNum;i++){
            //"https://3838.manhuacangku.com/RXUI/45380/762804.jpg";
            //"https://3838.manhuacangku.com/RXUI/45380/762834.jpg";
            //"https://file.3dtuman.com/h2/webp/2024051116/2619948_204.webp";
            String srcPath = "https://file.3dtuman.com/h2/webp/2024050620/2641653_" + i + ".webp";
            srcPaths.add(srcPath);
        }
        return deduplication(srcPaths);
    }

    private List<String> getSrcPathsByXFL() throws IOException {
        List<String> srcPaths = new ArrayList<>();
        int startNum = 0;
        int endNum = 28;
        for(int i = startNum;i <= endNum;i++){
            //"https://3838.manhuacangku.com/RXUI/45380/762804.jpg";
            //"https://3838.manhuacangku.com/RXUI/45380/762834.jpg";
            //"https://file.3dtuman.com/h2/webp/2024051116/2619948_204.webp";
            String srcPath = "https://file.3dtuman.com/h2/webp/2024050620/2641653_" + i + ".webp";
            srcPaths.add(srcPath);
        }
        return deduplication(srcPaths);
    }

    private List<String> deduplication(List<String> oldSrcPaths) {
        List<String> srcPaths = new ArrayList<>();
        for(String srcPath:oldSrcPaths){
            if(!srcPaths.contains(srcPath)){
                srcPaths.add(srcPath);
            }
        }
        return srcPaths;
    }
}
